package com.appcenter.application.mapper;

import com.appcenter.application.dto.permissions.PermissionsDto;
import com.appcenter.application.dto.roles.RoleDto;
import com.appcenter.application.input.permission.GetPermissionsByCokeInput;
import com.appcenter.application.input.permission.GetPermissionsInput;
import com.appcenter.application.input.permission.GetPermissionsServiceInput;
import com.appcenter.application.model.Dics;
import com.appcenter.application.model.Permissions;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface PermissionsMapper extends Mapper<Permissions> {

    //查询权限名称是否存在
    @Select("select * from permissions where ValidStatus=1 and AppId=#{AppId} AND PermissionCode=#{PermissionCode} ")
    public List<Permissions> getNameCount(@Param("AppId") String AppId,@Param("PermissionCode") String PermissionCode);

    //查询权限名称是否存在
    @Select("select * from permissions where ValidStatus=1 and AppId=#{AppId} AND PermissionCode=#{PermissionCode} and Id <> #{Id}")
    public List<Permissions> getNameCountNoId(@Param("AppId") String AppId,@Param("PermissionCode") String PermissionCode,@Param("Id") String Id);

    //查询当前应用下最大排序值
    @Select("select * FROM permissions WHERE ValidStatus=1 and AppId=#{AppId} ORDER BY ListOrder DESC limit 0,1")
    public Permissions maxListOrder(String AppId);

    //搜索查询信息
    @Select("<script>" +
            "select permissions.*,permissions.IsVisible visible FROM permissions join  apps " +
            "on  apps.Id=permissions.AppId " +
            ""+
            " <when test='appCode==&apos;JZ&apos; or  appCode==&apos;WE.EnterprisePortal&apos; '>" +
            " or permissions.AppId is null " +
            " </when>" +
            ""+
            "where permissions.ValidStatus=1 and permissions.IsVisible=1 and apps.ValidStatus=1 " +
            ""+
            " <when test='appCode!=null'>" +
            " AND apps.appCode=#{appCode}" +
            " </when>" +
            ""+
            ""+
         //  " <when test='appCode==JZ '>" +
         //  " AND permissions.AppId is  null" +
         //  " </when>" +
         //  ""+
          //  " <when test='keyword!=null'>" +
          //  " AND (PermissionName like concat('%',#{keyword},'%'))" +
          //  " </when>" +
          //  "" +
            " order by Level asc, ListOrder asc" +
            ""+
            "</script>")
    public List<Permissions> searchPermissionList(GetPermissionsInput input);

    //获取当前用户权限
    @Select({
            "<script>",
            "select DISTINCT p.* from  permissions p join getpermissions g on p.Id=g.permissionId " ,
            " and g.AppCode=#{appCode}" ,
             " and( p.AppId is null or p.AppId=(select Id from apps where appCode=#{appCode})) " ,
             "  where g.dataId in ",
            "<foreach collection='dataIds' item='dataId' open='(' separator=',' close=')'>",
            "#{dataId}",
            "</foreach>",
            "and p.ValidStatus=1 and p.IsVisible=1",
            " order by Level,ListOrder",
            "</script>"
    })
    public List<PermissionsDto> GetPermissions(@Param("dataIds") List<String> dataIds,@Param("appCode")String appCode);


    //获取当前用户权限
    @Select("<script>" +
                    "select DISTINCT p.* from permissions p " +
                    "inner JOIN getpermissions g on p.Id=g.permissionId " +
                    "inner join (select distinct e.Id from orgs e " +
                    "inner join (select a.JobId,a.OrgId,c.OrgIdPath from orgjobs a " +
                    "inner join jobusers b on a.Id=b.OrgJobId " +
                    "inner join orgs c on a.OrgId=c.Id " +
                    "inner join dataversion d on d.VersionNo=b.VersionNo and d.VersionNo=a.VersionNo  and d.VersionNo=c.VersionNo and d.VersionNo=b.VersionNo " +
                    "where b.UserId=#{userCode}) f " +
                    "inner join dataversion d on d.VersionNo=e.VersionNo  " +
                    "where f.OrgIdPath like concat(e.OrgIdPath,'%') " +
                    "UNION all " +
                    "select o.jobid from jobusers p  " +
                    "inner join orgjobs o on p.orgjobid=o.id " +
                    "inner join dataversion d on  d.VersionNo=p.VersionNo and  d.VersionNo=o.VersionNo " +
                    "where UserId=#{userCode} " +
                    "union ALL " +
                  //  "select Roleid from roleusers where UserId=#{userCode} " +
                   " select Roleid from roleusers rsu " +
                   "inner join roles rs on rsu.RoleId=rs.id " +
                   " where rs.AppId=(select Id from apps where appCode=#{appCode}) and UserId=#{userCode} and rsu.ValidStatus=1 " +

                    "union ALL " +
                    "select i.id from users i " +
                    "inner join dataversion d on d.VersionNo=i.VersionNo " +
                    "where UserCode=#{userCode}) u on u.id=g.dataId  " +
                    "and( p.AppId is null or p.AppId=(select Id from apps where appCode=#{appCode})) and g.AppCode=#{appCode} " +
                    "and p.ValidStatus=1 and p.IsVisible=1 " +
                    "order by Level,ListOrder " +
                    "</script>"
    )
    public List<PermissionsDto> GetPermissionsByCoke(GetPermissionsByCokeInput input);



    @Select("<script>" +
            " select a.* from permissions a" +
            " inner JOIN apps b on a.appid=b.id " +
            " where a.ValidStatus=1 and b.ValidStatus=1 and b.appcode= #{appCode}" +
            "" +
            "</script>")
    List<PermissionsDto> GetPermissionsAPI(@Param("appCode")String appCode);

}
